package com.chuangjiangx.member.score.ddd.domain.repository;

import com.chuangjiangx.commons.exception.BaseException;
import com.chuangjiangx.dddbase.Repository;
import com.chuangjiangx.member.basic.ddd.domain.model.*;
import com.chuangjiangx.member.score.dao.mapper.InMbrScoreGiftRecordMapper;
import com.chuangjiangx.member.score.dao.model.InMbrScoreGiftRecord;
import com.chuangjiangx.member.score.dao.model.InMbrScoreGiftRecordExample;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import com.chuangjiangx.member.score.ddd.domain.model.MbrScoreGiftRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * 会员兑换记录
 *
 * @author LeeXyan
 * @Date 2018/1/26 15:49
 **/
@Component
public class MbrScoreGiftRecordRepository implements Repository<MbrScoreGiftRecord, MbrScoreGiftRecordId> {

    @Autowired
    private InMbrScoreGiftRecordMapper inMbrScoreGiftRecordMapper;

    @Override
    public MbrScoreGiftRecord fromId(MbrScoreGiftRecordId mbrScoreGiftRecordId) {
        return null;
    }

    @Override
    public void update(MbrScoreGiftRecord mbrScoreGiftRecord) {
        Objects.requireNonNull(mbrScoreGiftRecord);
        InMbrScoreGiftRecord inMbrScoreGiftRecord = unbox(mbrScoreGiftRecord);
        inMbrScoreGiftRecordMapper.updateByPrimaryKeySelective(inMbrScoreGiftRecord);
    }

    @Override
    public void save(MbrScoreGiftRecord mbrScoreGiftRecord) {
        Objects.requireNonNull(mbrScoreGiftRecord);
        InMbrScoreGiftRecord inMbrScoreGiftRecord = unbox(mbrScoreGiftRecord);
        inMbrScoreGiftRecordMapper.insertSelective(inMbrScoreGiftRecord);
        mbrScoreGiftRecord.setId(new MbrScoreGiftRecordId(inMbrScoreGiftRecord.getId()));
    }


    public void deleteByMemberId(Long memberId) {
        InMbrScoreGiftRecordExample example = new InMbrScoreGiftRecordExample();
        example.createCriteria().andMemberIdEqualTo(memberId);
        List<InMbrScoreGiftRecord> list = inMbrScoreGiftRecordMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            list.forEach(inMbrScoreGiftRecord -> inMbrScoreGiftRecordMapper.deleteByPrimaryKey(inMbrScoreGiftRecord.getId()));
        }
    }

    /**
     * 根据取货码查询记录
     * @param claimCode 取货码
     * @return
     */
    public MbrScoreGiftRecord fromClaimCode(String claimCode,Long merchantId) {
        Objects.requireNonNull(claimCode);
        InMbrScoreGiftRecordExample example = new InMbrScoreGiftRecordExample();
        example.createCriteria()
                .andClaimCodeEqualTo(claimCode)
                .andMerchantIdEqualTo(merchantId);
        List<InMbrScoreGiftRecord> records = inMbrScoreGiftRecordMapper.selectByExample(example);
        if (records == null || records.size() == 0) {
            throw new BaseException("-1","无效的取货码");
        }
        if (records.size() > 1) {
            throw new BaseException("-1","积分兑换记录数据异常");
        }
        return wrap(records.get(0));
    }

    /**
     * 查询会员兑换次数
     * @param memberId
     * @param scoreGiftRuleId
     * @return
     */
    public long queryGiftCount(MemberId memberId, MbrScoreGiftRuleId scoreGiftRuleId){
        InMbrScoreGiftRecordExample example = new InMbrScoreGiftRecordExample();
        example.createCriteria()
                .andMemberIdEqualTo(memberId.getId())
                .andMemberScoreGiftRuleIdEqualTo(scoreGiftRuleId.getId());
        return inMbrScoreGiftRecordMapper.countByExample(example);

    }



    private InMbrScoreGiftRecord unbox(MbrScoreGiftRecord record) {
        Objects.requireNonNull(record);
        InMbrScoreGiftRecord in = new InMbrScoreGiftRecord();
        in.setId(record.getId() == null ? null : record.getId().getId());
        in.setMemberId(record.getMemberId() == null ? null : record.getMemberId().getId());
        in.setMemberScoreGiftRuleId(record.getMbrScoreGiftRuleId() == null ? null : record.getMbrScoreGiftRuleId().getId());
        in.setMerchantId(record.getMerchantId() == null ? null : record.getMerchantId().getId());
        in.setStoreId(record.getStoreId());
        in.setStoreUserId(record.getStoreUserId());
        in.setMerchantUserId(record.getMerchantUserId());
        in.setCreateTime(record.getTimestamp().getCreateTime());
        in.setUpdateTime(record.getTimestamp().getUpdateTime());
        in.setClaimCode(record.getClaimCode());
        in.setClaimStatus(record.getClaimStatus().code);
        in.setClaimTime(record.getClaimTime());
        in.setMbrScoreStreamId(null == record.getMbrScoreStreamId() ? null : record.getMbrScoreStreamId().getId());
        return in;
    }

    private MbrScoreGiftRecord wrap(InMbrScoreGiftRecord in) {
        return new MbrScoreGiftRecord(
                new MbrScoreGiftRecordId(in.getId()),new MemberId(in.getMemberId()),
                new MbrScoreGiftRuleId(in.getMemberScoreGiftRuleId()),
                new MerchantId(in.getMerchantId()),in.getStoreId(),
                in.getStoreUserId(),in.getMerchantUserId(),
                in.getCreateTime(),in.getUpdateTime(),in.getClaimCode(),
                ClaimStatus.getStatus(in.getClaimStatus()),in.getClaimTime(),
                null == in.getMbrScoreStreamId() ? null : new MbrScoreStreamId(in.getMbrScoreStreamId()));
    }
}
